package com.lwy.qiuchao.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.lwy.common.exception.BizCodeEnume;
import com.lwy.qiuchao.member.exception.PhoneExistException;
import com.lwy.qiuchao.member.exception.UserNameExistException;
import com.lwy.qiuchao.member.vo.MemberInfoVo;
import com.lwy.qiuchao.member.vo.MemberLoginVo;
import com.lwy.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lwy.qiuchao.member.entity.MemeberEntity;
import com.lwy.qiuchao.member.service.MemeberService;
import com.lwy.common.utils.PageUtils;
import com.lwy.qiuchao.member.vo.MemberRegistVo;
import com.lwy.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 *
 * @author lwy
 * @email ${email}
 * @date 2020-12-01 15:38:12
 */
@RestController
@RequestMapping("member/memeber")
public class MemeberController {
    @Autowired
     MemeberService memeberService;

    @GetMapping("/hasToken")
    public MemeberEntity hasToken(String username){
      return memeberService.hasToken(username);
    }

    @PostMapping("/modifyInfo")
    public R modifyInfo(MemeberEntity memeberEntity){
       int data =  memeberService.modifyInfo(memeberEntity);
       if(data==0){
           return R.error(BizCodeEnume.MEMBER_MODIFY_FAILED.getCode(),BizCodeEnume.MEMBER_MODIFY_FAILED.getMsg());
       }
        return R.ok();
    }

    @GetMapping("/queryInfoByToken")
    public MemeberEntity memberInfo(String token){
        return memeberService.memberInfoByToken(token);
    }
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo){
        MemeberEntity memeberEntity =  memeberService.login(memberLoginVo);
        if(memeberEntity!=null){
            return R.ok().setData(memeberEntity);
        }else {
            return R.error(BizCodeEnume.USERNAME_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnume.USERNAME_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }

    @GetMapping("/{mId}")
    public MemberInfoVo getMemberInfo(@PathVariable("mId")Long mId){
        MemberInfoVo memberInfoVo = memeberService.getMemberInfo(mId);
        return memberInfoVo;
    }


    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo memeberRegistVo) {
            try {
                memeberService.regist(memeberRegistVo);
            } catch (PhoneExistException e) {
                return R.error(BizCodeEnume.REGIST_PHONE_FAILED.getCode(), BizCodeEnume.REGIST_PHONE_FAILED.getMsg());
            } catch (UserNameExistException e) {
                return R.error(BizCodeEnume.REGIST_USERNAME_FAILED.getCode(), BizCodeEnume.REGIST_USERNAME_FAILED.getMsg());
            }
            return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memeberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{mId}")
    public R info(@PathVariable("mId") Long mId){
		MemeberEntity memeber = memeberService.getById(mId);

        return R.ok().put("memeber", memeber);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemeberEntity memeber){
		memeberService.save(memeber);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemeberEntity memeber){
		memeberService.updateById(memeber);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] mIds){
		memeberService.removeByIds(Arrays.asList(mIds));

        return R.ok();
    }

}
